#!/usr/bin/perl

open(OUTPUT, "> fsdata.h");

chdir("fs");
open(FILES, "find . -type f |");

while($file = <FILES>) {

    # Do not include files in CVS directories nor backup files.
    if($file =~ /(CVS|~)/) {
    	next;
    }
    
    chop($file);
    $flag = "0";
    
    if($file =~ /\.html$/) {
	$mime = "text/html; charset=utf-8";
    } elsif($file =~ /\.dhtml$/) {
  $flag = "1";
	$mime = "text/html; charset=utf-8";
    } elsif($file =~ /\.gif$/) {
	$mime = "image/gif";
    } elsif($file =~ /\.png$/) {
	$mime = "image/png";
    } elsif($file =~ /\.jpg$/) {
	$mime = "image/jpeg";
    } elsif($file =~ /\.class$/) {
	$mime = "application/octet-stream";
    } elsif($file =~ /\.ram$/) {
  $mime = "audio/x-pn-realaudio";
    } else {
	$mime = "text/plain";
    }

    open(FILE, "$file");

    $file =~ s/\.//;
    
    $fvar = $file;
    $fvar =~ s-/-_-g;
    $fvar =~ s-\.-_-g;
    print(OUTPUT "static const unsigned char data".$fvar."[] = {\n");
    print(OUTPUT "\t/* $file */\n\t");
    
    $i = 0;
    while(read(FILE, $data, 1)) {
        if($i == 0) {
            print(OUTPUT "\t");
        }
        printf(OUTPUT "%#02x, ", unpack("C", $data));
        $i++;
        if($i == 10) {
            print(OUTPUT "\n");
            $i = 0;
        }
    }
    print(OUTPUT "};\n\n");
    close(FILE);
    push(@fvars, $fvar);
    push(@files, $file);
    push(@flags, $flag);
    push(@mime, $mime);
}

print(OUTPUT "static struct vfs_entry_s vfs_entries[]={\n");

for($i = 0; $i < @fvars; $i++) {
    $file = $files[$i];
    $fvar = $fvars[$i];
    $mime = $mime[$i];
    $flag = $flags[$i];
    print(OUTPUT "\t{\"$file\", data$fvar, sizeof(data$fvar), \"$mime\", $flag}, \n");
}

print(OUTPUT "\t{0, 0, 0, 0}\n};\n");
